package com.prs.crm.domain.stock;

import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.Cascade;

import com.prs.crm.domain.Currency;
import com.prs.crm.domain.FlowCodeEntity;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.domain.store.StoreIn;

/**
 * 采购单
 * 
 * @author prs.chenzhi
 * 
 */
@Entity
@Table(name = "stock_purchase")
public class Purchase extends FlowCodeEntity {
	private static final long serialVersionUID = 1L;
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Integer id;
	/**
	 * 采购主题
	 */
	private String name;

	/**
	 * 采购编号
	 */
	private String purchaseCode;
	/**
	 * 供应商
	 */
	private String supplier;

	/**
	 * 币种
	 */
	@ManyToOne
	private Currency currency;

	/**
	 * 采购分类
	 */
	@ManyToOne
	private PurchaseType type;

	/**
	 * 采购日期
	 */
	private Date purchaseDate;

	/**
	 * 参与人
	 */
	@ManyToMany(fetch = FetchType.LAZY)
	@OrderBy
	private Set<Person> joiners;

	/**
	 * 负责人
	 */
	@ManyToOne
	private Person principal;

	@ManyToOne
	private StoreIn storein;

	@Transient
	private Map<String, String> valueMap;

	/**
	 * 采购明细
	 */
	@OneToMany(mappedBy = "purchase")
	@OrderBy
	@Cascade( { org.hibernate.annotations.CascadeType.ALL,
			org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
	private Set<PurchaseDetail> details;

	/**
	 * 采购说明
	 */
	@Lob
	private String remark;

	@Column(columnDefinition = "Integer(11) default '0'")
	private int iquiryTag;
	
	/**
	 * 附件
	 */
private String purchaseAttach;
	

	public String getPurchaseAttach() {
		return purchaseAttach;
	}

	public void setPurchaseAttach(String purchaseAttach) {
		this.purchaseAttach = purchaseAttach;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public Integer getId() {
		return id;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public String getPurchaseCode() {
		return purchaseCode;
	}

	public void setPurchaseCode(String purchaseCode) {
		this.purchaseCode = purchaseCode;
	}

	public void setCurrency(Currency currency) {
		this.currency = currency;
	}

	public Currency getCurrency() {
		return currency;
	}

	public void setType(PurchaseType type) {
		this.type = type;
	}

	public PurchaseType getType() {
		return type;
	}

	public void setPurchaseDate(Date purchaseDate) {
		this.purchaseDate = purchaseDate;
	}

	public Date getPurchaseDate() {
		return purchaseDate;
	}

	public void setJoiners(Set<Person> joiners) {
		this.joiners = joiners;
	}

	public Set<Person> getJoiners() {
		return joiners;
	}

	public void setPrincipal(Person principal) {
		this.principal = principal;
	}

	public Person getPrincipal() {
		return principal;
	}

	public void setDetails(Set<PurchaseDetail> details) {
		this.details = details;
	}

	public Set<PurchaseDetail> getDetails() {
		return details;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public String getRemark() {
		return remark;
	}

	public void addDetail(PurchaseDetail detail) {
		if (details == null)
			details = new HashSet<PurchaseDetail>();
		detail.setPurchase(this);
		details.add(detail);
	}

	public String getSupplier() {
		return supplier;
	}

	public void setSupplier(String supplier) {
		this.supplier = supplier;
	}

	public void addCopyToPersons(Collection<Person> persons) {
		if (joiners == null) {
			joiners = new HashSet<Person>();
		}
		if (persons != null) {
			joiners.addAll(persons);
		}
	}

	public StoreIn getStorein() {
		return storein;
	}

	public void setStorein(StoreIn storein) {
		this.storein = storein;
	}

	public Map<String, String> getValueMap() {
		return valueMap;
	}

	public void setValueMap(Map<String, String> valueMap) {
		this.valueMap = valueMap;
	}

	public int getIquiryTag() {
		return iquiryTag;
	}

	public void setIquiryTag(int iquiryTag) {
		this.iquiryTag = iquiryTag;
	}
}
